from tkinter import *
import os
from selenium import webdriver,common
import datetime
import time

global USERNAME
global USERPASS
global TARGETRANGE
global PAYPASS
global SHENG
global SHI
global QU

#=======================================
USERNAME = ""
USERPASS = ""
TARGETRANGE = ""
PAYPASS = ""
SHENG = ""
SHI = ""
QU = ""
#=======================================
class MyThread():

	def run(self):

		global ADDRANGE

		def closeAD(value):
			bool_ViewAD = False
			while(not bool_ViewAD):
				try:
					value.find_element_by_class_name("colse").click()
					bool_ViewAD = True
				except(common.exceptions.NoSuchElementException,common.exceptions.ElementNotInteractableException):
					time.sleep(0.01)

		def doSign(value):
			if(value.find_element_by_xpath('//uni-page-body/uni-view[1]/uni-view[4]').is_displayed()):
				value.find_element_by_class_name("que").click()
				time.sleep(1)
				value.find_element_by_xpath('//uni-button[contains(text(),"我已阅读完所有内容，同意进行签字")]').click()
				time.sleep(1)
				#跳过具体书写部分，直接确认
				value.find_element_by_xpath('//uni-view[contains(text(),"确认")]').click()
				time.sleep(1)
				value.find_element_by_xpath('//uni-button[contains(text(),"提交")]').click()
				time.sleep(1)
				value.find_element_by_xpath('//uni-page-body/uni-view[1]/uni-view[4]/uni-button[1]').click()
				time.sleep(2)

		#打开网页
		browser = webdriver.Chrome()
		browser.get('https://mobile.zxxm.top')

		#关闭弹窗

		time.sleep(5)

		#closeAD(browser)

		browser.find_element_by_xpath('//*[@class="uni-tabbar"]/div[3]').click()

		#检测是否正确弹到登陆界面
		bool_ViewUserAndPwd = False
		while(not bool_ViewUserAndPwd):
			try:
				browser.find_element_by_xpath('//uni-page-body/uni-view[1]/uni-view[4]/uni-input[1]/div[1]/input[1]').clear()
				browser.find_element_by_xpath('//uni-page-body/uni-view[1]/uni-view[4]/uni-input[1]/div[1]/input[1]').send_keys(USERNAME)
				browser.find_element_by_xpath('//uni-page-body/uni-view[1]/uni-view[5]/uni-input[1]/div[1]/input[1]').clear()
				browser.find_element_by_xpath('//uni-page-body/uni-view[1]/uni-view[5]/uni-input[1]/div[1]/input[1]').send_keys(USERPASS)
				browser.find_element_by_xpath('//uni-page-body/uni-view[1]/uni-view[6]/uni-radio[1]/div[1]').click()
				try:
					while(browser.find_element_by_xpath('//uni-page-body/uni-view[1]/uni-view[7]')):
						time.sleep(1)
						browser.find_element_by_xpath('//uni-page-body/uni-view[1]/uni-view[7]').click()
						time.sleep(1)
				except(common.exceptions.NoSuchElementException,common.exceptions.ElementNotInteractableException):
					bool_ViewUserAndPwd = True
			except(common.exceptions.NoSuchElementException,common.exceptions.ElementNotInteractableException):
				time.sleep(0.01)

		#关闭弹窗
		#closeAD(browser)

		browser.find_element_by_xpath('//*[@class="uni-tabbar"]/div[4]').click()
		time.sleep(2)

		#签字确认
		doSign(browser)

		#选择省市区
		#省
		bool_SelectArea = False
		while(not bool_SelectArea):
			try:
				time.sleep(2)
				#打开左上角地区选择
				browser.find_element_by_xpath('//uni-page-body/uni-view[1]/uni-view[1]/uni-view[1]/uni-view[1]/uni-view[1]/uni-image[1]/div[1]').click()
				bool_SelectArea = True
			except(common.exceptions.NoSuchElementException,common.exceptions.ElementNotInteractableException):
				time.sleep(0.01)
		bool_Sheng = False
		while(not bool_Sheng):
			try:

				global SHENG
				global SHI
				global QU

				time.sleep(0.1)
				#选择省、市、区

				#JS_ToShengView =
				#'document.getElementsByClassName("select-area-xzsq")[0].childNodes[0].click()'

				#browser.execute_script(JS_ToShengView)

				time.sleep(0.1)

				ShengPath = 'var a =document.getElementsByClassName("select-area-cslb")[0];for (var i = 0; i <a.childElementCount; i++){if(a.children[i].children[0].children[0].innerText=="' + SHENG + '"){a.children[i].click();break;}}'

				browser.execute_script(ShengPath)

				time.sleep(0.1)

				JS_ToShiView = 'document.getElementsByClassName("select-area-xzsq")[0].childNodes[1].click()'

				browser.execute_script(JS_ToShiView)

				time.sleep(0.1)

				#browser.find_element_by_xpath('//body[1]/uni-app[1]/uni-page[1]/uni-page-wrapper[1]/uni-page-body[1]/uni-view[1]/uni-view[3]/uni-view[1]/uni-view[1]/uni-view[2]/uni-view[2]/uni-view[contains(@class,"select-area-csnr")]/uni-view[1]/uni-text[1]/span[contains(text(),"'
				#+ SHI + '")]').click()

				ShiPath = 'var a = document.getElementsByClassName("select-area-cslb")[0];for (var i = 0; i < a.childElementCount; i++) {if(a.children[i].children[0].children[0].innerText=="' + SHI + '"){a.children[i].click();break;}}'

				browser.execute_script(ShiPath)

				time.sleep(0.1)

				JS_ToQuView = 'document.getElementsByClassName("select-area-xzsq")[0].childNodes[2].click()'

				browser.execute_script(JS_ToQuView)

				time.sleep(0.1)

				#browser.find_element_by_xpath('/html[1]/body[1]/uni-app[1]/uni-page[1]/uni-page-wrapper[1]/uni-page-body[1]/uni-view[1]/uni-view[3]/uni-view[1]/uni-view[1]/uni-view[2]/uni-view[2]/uni-view[contains(@class,"select-area-csnr")]/uni-view[1]/uni-text[1]/span[contains(text(),"'
				#+ QU + '")]').click()

				QuPath = 'var a = document.getElementsByClassName("select-area-cslb")[0];for (var i = 0; i < a.childElementCount; i++) {if(a.children[i].children[0].children[0].innerText=="' + QU + '"){a.children[i].click();break;}}'

				browser.execute_script(QuPath)

				time.sleep(0.1)

				#确认
				browser.find_element_by_xpath('//uni-page-body/uni-view[1]/uni-view[3]/uni-view[1]/uni-view[1]/uni-view[2]/uni-view[3]/uni-button[1]').click()

				bool_Sheng = True
			except(common.exceptions.NoSuchElementException,common.exceptions.ElementNotInteractableException):
				time.sleep(0.01)

		#-------------------------------
		#测试断点时延
		#time.sleep(1000)
		#-------------------------------

		global TARGETRANGE

		#竞拍按钮点击
		bool_CompeteClick = False
		while(not bool_CompeteClick):
			try:
				time.sleep(0.01)
				browser.find_element_by_xpath('//uni-page-body/uni-view[1]/uni-view[2]/uni-view[1]/uni-view[6]').click()
				time.sleep(0.01)
				if(browser.find_element_by_xpath('//uni-page-body/uni-view[1]/uni-view[5]').is_displayed()):
					print("fight!")
					#============================================
					#开始竞拍操作
					curRange = 20

					while(curRange < int(TARGETRANGE)):
						browser.find_element_by_xpath('//uni-page-body/uni-view[1]/uni-view[5]/uni-view[1]/uni-view[1]/uni-view[2]/uni-view[4]/uni-image[2]').click()
						time.sleep(0.01)
						curRange+=10
					browser.find_element_by_xpath('//uni-page-body/uni-view[1]/uni-view[5]/uni-view[1]/uni-view[1]/uni-view[3]/uni-button[1]').click()
					bool_CompeteClick = True
				else:
					print("Checking...")
			except(common.exceptions.NoSuchElementException,common.exceptions.ElementNotInteractableException):
				time.sleep(0.01)

		bool_ClickPay = False
		while(not bool_ClickPay):
			try:
				time.sleep(0.01)
				if(browser.find_element_by_xpath('//uni-page-body/uni-view[1]/uni-view[6]').is_displayed()):
					time.sleep(0.1)
					browser.find_element_by_xpath('//uni-page-body/uni-view[1]/uni-view[6]/uni-view[2]/uni-view[2]/uni-input[1]/div[1]/input[1]').clear()
					time.sleep(0.1)
					browser.find_element_by_xpath('//uni-page-body/uni-view[1]/uni-view[6]/uni-view[2]/uni-view[2]/uni-input[1]/div[1]/input[1]').send_keys(PAYPASS)
					time.sleep(0.1)
					JS_ClickPay = 'document.getElementsByClassName("zfan")[0].children[0].click()'
					browser.execute_script(JS_ClickPay)
					bool_ClickPay = True
			except(common.exceptions.NoSuchElementException,common.exceptions.ElementNotInteractableException):
				time.sleep(0.01)
		time.sleep(0.01)
		print("Is Done...")
		print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

		#关闭浏览器
		#browser.quit()
		time.sleep(1000)

if __name__ == '__main__':
			
	root = Tk()

	root.title("自动抢拍代理 v1.0")

	root.minsize(450,300)
	root.maxsize(450,300)

	Label(root,text="用户名：").grid(row=0,column=0)
	Label(root,text="密码：").grid(row=1,column=0)
	Label(root,text="增幅：").grid(row=2,column=0)
	Label(root,text="支付密码：").grid(row=3,column=0)
	Label(root,text="省：").grid(row=4,column=0)
	Label(root,text="按照平台上的显示内容输入").grid(row=4,column=2)
	Label(root,text="市：").grid(row=5,column=0)
	Label(root,text="按照平台上的显示内容输入").grid(row=5,column=2)
	Label(root,text="区：").grid(row=6,column=0)
	Label(root,text="按照平台上的显示内容输入").grid(row=6,column=2)

	e1 = Entry(root)
	e1.grid(row=0,column=1,padx=10,pady=5)
	e2 = Entry(root)
	e2.grid(row=1,column=1,padx=10,pady=5)
	e3 = Entry(root)
	e3.grid(row=2,column=1,padx=10,pady=5)
	e4 = Entry(root)
	e4.grid(row=3,column=1,padx=10,pady=5)
	e5 = Entry(root)
	e5.grid(row=4,column=1,padx=10,pady=5)
	e6 = Entry(root)
	e6.grid(row=5,column=1,padx=10,pady=5)
	e7 = Entry(root)
	e7.grid(row=6,column=1,padx=10,pady=5)

	def setDefaultInfo():
		e1.insert(0,"")
		e2.insert(0,"")
		e3.insert(0,TARGETRANGE)
		e4.insert(0,"")
		e5.insert(0,"")
		e6.insert(0,"")
		e7.insert(0,"")
	def getInfo():
		global USERNAME
		global USERPASS
		global TARGETRANGE
		global PAYPASS
		global SHENG
		global SHI
		global QU
		USERNAME = e1.get()
		USERPASS = e2.get()
		TARGETRANGE = e3.get()
		PAYPASS = e4.get()
		SHENG = e5.get()
		SHI = e6.get()
		QU = e7.get()
	
	setDefaultInfo()

	def startFight():
		getInfo()
		a = MyThread()
		a.run()

	Button(root,text="开始",width=10,command=startFight).grid(row=7,column=0,sticky=W,padx=10,pady=5)

	root.mainloop()